package servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;

import ds.IClientDS;
import entities.User;

/**
 * Servlet implementation class TempsReel
 */
@WebServlet("/connection")
public class Connection extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Connection() {
		super();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// Log out request
		if (request.getParameter("logout") != null) {
			request.getSession().setAttribute("user", null);
		}

		// redirect to the login page
		response.sendRedirect("/myList/jsp/index.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// Get the loaded context
		WebApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());

		// Instanciate the domain service ClientDS
		IClientDS clientDS = (IClientDS) ctx.getBean("clientDS");

		
		
//		FacebookClient facebookClient = new DefaultFacebookClient(accessToken)
		
		// get the login from the request
		String login = request.getParameter("login");

		// Connection request
		if ("login".equals(request.getParameter("action"))) {
			// check if the couple login pass is good
			if (clientDS.checkLoginPass((String) login,
					(String) request.getParameter("pwd"))) {
				// if it's good, save the user in the session
				request.getSession(true).setAttribute("user",
						clientDS.find(login));
				// redirect to the home page
				response.sendRedirect("/myList/ListManager");
			} else {
				// else redirect to the form with an error
				response.sendRedirect("/myList/jsp/index.jsp?error=fail");

			}
		}

		// Account creation request
		else if ("register".equals(request.getParameter("action"))) {
			// check if the login is available
			if (clientDS.isAvailable(login)) {
				// check if the two passwords are equals
				if (request.getParameter("pass1").equals(
						request.getParameter("pass2"))) {

					// if everything is good, create the new user account
					User user = new User();
					user.setLogin(login);
					user.setPassword(request.getParameter("pass1"));
					user.setFirstName(request.getParameter("firstName"));
					user.setLastName(request.getParameter("lastName"));

					clientDS.createUser(user);

					request.getSession(true).setAttribute("user", user);
					response.sendRedirect("/myList/ListManager");
				} else {
					// if the passwords are differents, redirect to the form
					response.sendRedirect("/myList/jsp/register.jsp?error=passDifferent");
				}
			} else {
				// if the login is not available, redirect to the form
				response.sendRedirect("/myList/jsp/register.jsp?error=loginExists");
			}
		}
	}

}
